﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KantoorDienst
{
    public partial class minutionEdit : Form
    {
        DBConnect db;
        public minutionEdit()
        {
            InitializeComponent();
            db = new DBConnect();
        }

        private void etcEdit_Load(object sender, EventArgs e)
        {
            getMunition();
            getCalibers();
            fillFields();
        }

        private void getMunition()
        {
            itemListCmb.DisplayMember = "name";
            itemListCmb.ValueMember = "prod_id";
            itemListCmb.DataSource = db.getMunitionList();
        }
        private void getCalibers()
        {
            caliberBox.DisplayMember = "name";
            caliberBox.ValueMember = "cal_id";
            caliberBox.DataSource = db.getCaliberList();
            caliberBox.SelectedValue = ((DataRowView)itemListCmb.SelectedItem)["cal_id"]; //get selected caliber from currently selected weapon
        }
        private void fillFields()
        {
            weaponNameTxt.Text = ((DataRowView)itemListCmb.SelectedItem)["name"].ToString();
            barcodeTxt.Text = ((DataRowView)itemListCmb.SelectedItem)["barcode"].ToString();
            weaponNumberTxt.Text = ((DataRowView)itemListCmb.SelectedItem)["item_no"].ToString();
            caliberBox.SelectedValue = ((DataRowView)itemListCmb.SelectedItem)["cal_id"]; //get selected caliber from currently selected weapon
            priceBox.Text = ((DataRowView)itemListCmb.SelectedItem)["price"].ToString();
            stockBox.Text = ((DataRowView)itemListCmb.SelectedItem)["stock"].ToString();
            checkBox1.Checked = Boolean.Parse(((DataRowView)itemListCmb.SelectedItem)["permit"].ToString());
        }

        private void newBtn_Click(object sender, EventArgs e)
        {
            db.newItem();
            getMunition();
            itemListCmb.SelectedIndex = itemListCmb.Items.Count - 1;
            caliberBox.SelectedIndex = 0;
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Bijvoorbeeld een nummer dat op de minutie staat, zodat verschillende batches minutie van hetzelfde merk uit elkaar te houden zijn.\nOptionele waarde.");
        }

        private void itemListCmb_SelectedIndexChanged(object sender, EventArgs e)
        {
            fillFields();
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            priceBox.Text = priceBox.Text.Replace(",", ".");
            int checkbox = 0;
            if (checkBox1.Checked) checkbox = 1;
            db.setItem(weaponNameTxt.Text, barcodeTxt.Text, priceBox.Text, caliberBox.SelectedValue.ToString(), stockBox.Text, itemListCmb.SelectedValue.ToString(), checkbox);
            getMunition();
        }
    }
}
